// ----------------
// Project:
// ESA Elevator
// ----------------
//
// ----------------
// Group: 163
// 2358569: Pascal Klink
// 2649456: Lukas Kreh
// ----------------
//
// Description:
// ----------------
// elevator user_ctrl
//
// Version History:
// ----------------
// 140116: set parameter FLOORS to 30
// 140116: set parameter FLOOR_BITS to 5

`timescale 1ns / 1ns

module user_ctrl

          #(parameter FLOORS     = 30,
                      FLOOR_BITS =  5)


          (input  wire                    CLK,
           input  wire                    RESET,
           
           input  wire [(FLOOR_BITS-1):0] CURRENT_FLOOR_IN,           // cabin stage
           input  wire                    HALTED,
           input  wire [(FLOORS-1)    :0] FLOOR_REQUEST,              // floor button pressed
           input  wire                    MANUAL_DOOR_CLOSE_IN,       // close button pressed  
           input  wire                    MANUAL_DOOR_OPEN_IN,        // open button pressed         
           input  wire                    MANUAL_ALARM_IN,            // alarm button pressed

           output wire [(FLOOR_BITS-1):0] CURRENT_FLOOR_OUT,          // forward to cabin display
           output wire                    MANUAL_DOOR_CLOSE_OUT,      // door close cmd
           output wire                    MANUAL_DOOR_OPEN_OUT,       // door open cmd
           output wire                    MANUAL_ALARM_OUT,           // user alarm         
           output wire [(FLOORS-1)    :0] DESTINATIONS,               // destinations
           output reg  [(FLOOR_BITS-1):0] CLEAR_FLOOR_BUTTON,         // reset_button
           output reg                     CLEAR_FLOOR_BUTTON_VALID);  // validate reset_button

/* =============================INSERT CODE HERE======================================*/ 
localparam DISPLAY_SEGMENTS=14;
wire [(FLOORS-1):0] enlight;				
wire [(DISPLAY_SEGMENTS-1):0] segments;
reg [(FLOOR_BITS-1):0] current;
wire door_open;
wire door_close;
wire alarm;
	//connect keyboard
	keyboard_if #(
					.FLOORS     (FLOORS),
					.FLOOR_BITS (FLOOR_BITS))
	KEYBOARD_IF_i (
					.CLK(CLK),
					.RESET(RESET),
					.FLOOR_SELECT(FLOOR_REQUEST),
					.CLOSE_DOOR_IN(MANUAL_DOOR_CLOSE_IN),
					.OPEN_DOOR_IN(MANUAL_DOOR_OPEN_IN),
					.PASSENGER_ALARM_IN(MANUAL_ALARM_IN),
					
					.CLEAR_FLOOR_BUTTON(CLEAR_FLOOR_BUTTON),
					.CLEAR_FLOOR_BUTTON_VALID(CLEAR_FLOOR_BUTTON_VALID),

					.CLOSE_DOOR_OUT(door_close),
					.OPEN_DOOR_OUT(door_open),
					.PASSENGER_ALARM_OUT(MANUAL_PASSENGER_ALARM_OUT),
					.SELECTED_FLOORS(DESTINATIONS),
					.ENLIGHT_BUTTONS(enlight));

	//connect 7-segment-displays
	display_if #(
					.FLOOR_BITS       (FLOOR_BITS),
					.DISPLAY_SEGMENTS (DISPLAY_SEGMENTS))     
	DISPLAY_IF_i (            
					.CURRENT_FLOOR  (CURRENT_FLOOR_IN),
					.ENABLE_SEGMENT (segments));
assign CURRENT_FLOOR_OUT=current;
//Door can only be opened/closed if halted
assign MANUAL_DOOR_CLOSE_OUT=(HALTED && door_close);
assign MANUAL_DOOR_OPEN_OUT=(HALTED && door_open);
always@(posedge CLK)begin
	if(RESET)begin
		CLEAR_FLOOR_BUTTON=0;
		CLEAR_FLOOR_BUTTON_VALID=0;
		current=CURRENT_FLOOR_IN;
	end
	else begin
		//clear floor if halted
		if(HALTED)begin 
			current=CURRENT_FLOOR_IN;		//position is exact if halted=1
			CLEAR_FLOOR_BUTTON=CURRENT_FLOOR_IN;
			CLEAR_FLOOR_BUTTON_VALID=1;
		end
		else begin
			CLEAR_FLOOR_BUTTON_VALID=0;
		end
	end
end

/* ====================================================================================*/

endmodule
